package com.fxu.jushuitan.dto;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.Data;

import java.util.Date;
import java.util.List;

/**
 * 所有分页返回
 * @author fangxu
 * @since 2023-08-18
 * @version 1.0.0
 **/
@Data
public class PageReq {
    //"{\"page_index\":1,\"page_size\":30,\"date_type\":0,\"so_ids\":[],\"modified_begin\":\"2023-01-02 15:55:06\",\"modified_end\":\"2023-01-09 15:55:06\"}";

    /**
     * 第几页
     */
    private Integer pageIndex;

    /**
     * 每页个数
     */
    private Integer pageSize = 30;

    /**
     * 日期类型，0:修改时间，1:创建日期，2:订单日期，4:实际出/入库时间
     */
    private Integer dateType = 0;

    /**
     * 业务ID集合
     */
    private List<String> soIds;

    /**
     * 开始时间
     */
    private Date modifiedBegin;

    /**
     * 结束时间
     */
    private Date modifiedEnd;

    public PageReq(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }

    public PageReq(Integer pageIndex, Integer pageSize) {
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
    }

    public PageReq(Integer pageIndex, Integer pageSize, List<String> soIds) {
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.soIds = soIds;
    }

    public PageReq(Integer pageIndex, List<String> soIds) {
        this.pageIndex = pageIndex;
        this.soIds = soIds;
    }

    public PageReq(Integer pageIndex, Date begin, Date end) {
        this.pageIndex = pageIndex;
        this.modifiedBegin = begin;
        this.modifiedEnd = end;
    }

    public PageReq(Integer pageIndex, String begin, String end) {
        this.pageIndex = pageIndex;
        this.modifiedBegin = DateUtil.parse(begin);
        this.modifiedEnd = DateUtil.parse(end);
    }

    public String toBiz() {
        String tmp = "{\"page_index\":{},\"page_size\":{},\"date_type\":{}";
        tmp = StrUtil.format(tmp, pageIndex, pageSize, dateType);
        if (soIds != null) {
            tmp += ",\"so_ids\":\"" + JSONUtil.toJsonStr(soIds) + "\"";
        }
        if (modifiedBegin != null) {
            tmp += ",\"modified_begin\":\"" + modifiedBegin + "\"";
        }
        if (modifiedEnd != null) {
            tmp += ",\"modified_end\":\"" + modifiedEnd + "\"";
        }
        return tmp + "}";
    }
}